package com.jae.controller.resc;


import com.jae.controller.BaseController;
import com.jae.model.ResultMsg;
import com.jae.model.ServiceRuntimeException;
import com.jae.model.resc.Resc;
import com.jae.model.user.User;
import com.jae.service.RescService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 权限Controller
 * 
 * @author Jae
 * @email 1571878499@qq.com
 * @date 2020-12-01 09:22:00
 */
@RestController
@RequestMapping("/admin/resc")
public class RescAdminController extends BaseController {

	@Autowired
	private RescService rescService;

	/**
	 * /admin/resc/info 获取指定权限信息
	 */
	@RequestMapping(value = "/info")
	public ResultMsg info(HttpServletRequest request, Long resc_id) {
		String interfaceName = "获取指定权限信息";
		try {
			return new ResultMsg(true, rescService.findById(resc_id));
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

	/**
	 * /admin/resc/add 添加权限
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ResultMsg add(HttpServletRequest request, Resc resc) {
		String interfaceName = "添加权限";
		try {
			User user = getUser();
			return new ResultMsg(true, rescService.insert(resc, user));
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

	/**
	 * /admin/resc/update 修改权限
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResultMsg update(HttpServletRequest request, Resc resc) {
		String interfaceName = "修改权限";
		try {
			User user = getUser();
			if(rescService.update(resc, user))
				return new ResultMsg(true);
			return new ResultMsg(false,"修改失败" );
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

	/**
	 * /admin/resc/delete 物理删除权限
	 */
	@RequestMapping(value = "/delete")
	public ResultMsg delete(HttpServletRequest request, Long resc_id) {
		String interfaceName = "物理删除权限";
		try {
			if (rescService.delete(resc_id))
				return new ResultMsg(true);
			return new ResultMsg(false, "失败");
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

}
